package com.jackrain.nea.omslocal.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.aliyun.openservices.ons.api.Action;
import com.aliyun.openservices.ons.api.ConsumeContext;
import com.aliyun.openservices.ons.api.Message;
import com.aliyun.openservices.ons.api.MessageListener;
import com.aliyun.openservices.ons.api.impl.util.MsgConvertUtil;
import com.jackrain.nea.config.PropertiesConf;
import com.jackrain.nea.omslocal.Dao.*;
import com.jackrain.nea.omslocal.service.SaveSkuErrservice;
import com.jackrain.nea.omslocal.service.SendMqServies;
import com.jackrain.nea.util.ApplicationContextHandle;
import lombok.extern.slf4j.Slf4j;

import java.io.PrintWriter;
import java.io.StringWriter;

/**
 * @Discription:
 *  订单消息 listener
 * Created with IntelliJ IDEA.
 * @Author cs
 * @Date 2017/10/10 10:37
 */
@Slf4j
public class OrderMsgListener implements MessageListener {


    @Override
    public Action consume(Message message, ConsumeContext consumeContext) {

        SendMqServies sendMq = ApplicationContextHandle.getBean(SendMqServies.class);
        PropertiesConf propertiesConf = ApplicationContextHandle.getBean(PropertiesConf.class);
        TaobaoOrderDao taobaoOrderDao = ApplicationContextHandle.getBean(TaobaoOrderDao.class);
        TaoBaoRefundDao taoBaoRefundDao = ApplicationContextHandle.getBean(TaoBaoRefundDao.class);
        TaoBaoFxOrder taoBaoFxOrder = ApplicationContextHandle.getBean(TaoBaoFxOrder.class);
        TaoBaoItemDao taoBaoItemDao = ApplicationContextHandle.getBean(TaoBaoItemDao.class);
        TaoBaoJxOrderDao taoBaoJxOrderDao = ApplicationContextHandle.getBean(TaoBaoJxOrderDao.class);
        TaoBaoFXRefundDao taoBaoFXRefundDao = ApplicationContextHandle.getBean(TaoBaoFXRefundDao.class);

        // 获取 流
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        try {

            if (message.getTopic().equals(propertiesConf.getProperty("ons.orderTopic"))) {
                JSONArray orderArr = JSONArray.parseArray(MsgConvertUtil.objectDeserialize(message.getBody()).toString());
                taobaoOrderDao.orderStart(orderArr, message.getKey(), message.getTopic());
                log.debug(message.getTopic()+ "===" +message.getTag()+" === "+message.getKey());

            } else if (message.getTopic().equals(propertiesConf.getProperty("ons.refundTopic"))) {
                // 消费退单
                JSONArray refundArr = JSONArray.parseArray(MsgConvertUtil.objectDeserialize(message.getBody()).toString());
                taoBaoRefundDao.refundStart(refundArr, message.getKey(), message.getTopic());
                log.debug(message.getTopic()+ "===" +message.getTag()+" === "+message.getKey());

            } else if (message.getTopic().equals(propertiesConf.getProperty("ons.fxOrderTopic"))) {

                JSONArray fxOrderArr = JSONArray.parseArray(MsgConvertUtil.objectDeserialize(message.getBody()).toString());
                taoBaoFxOrder.orderStart(fxOrderArr, message.getKey(), message.getTopic());
                log.debug(message.getTopic()+ "===" +message.getTag()+" === "+message.getKey());

            } else if (message.getTopic().equals(propertiesConf.getProperty("ons.itemTopic"))) {

                JSONArray itemArr = JSONArray.parseArray(MsgConvertUtil.objectDeserialize(message.getBody()).toString());
                taoBaoItemDao.itemStart(itemArr, message.getKey(), message.getTopic());
                log.debug(message.getTopic()+ "===" +message.getTag()+" === "+message.getKey());

            } else if (message.getTopic().equals(propertiesConf.getProperty("ons.fxRefundTopic"))) {

                JSONArray fxRefundArr = JSONArray.parseArray(MsgConvertUtil.objectDeserialize(message.getBody()).toString());
                taoBaoFXRefundDao.refundStart(fxRefundArr, message.getKey(), message.getTopic());
                log.debug(message.getTopic()+ "===" +message.getTag()+" === "+message.getKey());

            } else if (message.getTopic().equals(propertiesConf.getProperty("ons.jxOrderTopic"))) {
                JSONArray jxOrderArr = JSONArray.parseArray(MsgConvertUtil.objectDeserialize(message.getBody()).toString());
                taoBaoJxOrderDao.orderStart(jxOrderArr, message.getKey(), message.getTopic());
                log.debug(message.getTopic()+ "===" +message.getTag()+" === "+message.getKey());
            }  else {
                log.error("没有对应的消费topic,消费失败");
                return Action.ReconsumeLater;
            }

        } catch (Exception e) {
            log.error("消费报错========");
            // 逐条将错误信息以消息发送到线上发送到线上
            JSONObject errObj = new JSONObject();
            errObj.put("topic", message.getTopic());
            errObj.put("tag", propertiesConf.getProperty("ons.comp"));
            errObj.put("msgkey", message.getKey());
            // 将错误以流写出
            e.printStackTrace(pw);
            errObj.put("code",-1);
            errObj.put("errmsg", sw.toString());
            errObj.put("protime",System.currentTimeMillis());
            try {
                sendMq.sendMqinfoObj(errObj, propertiesConf.getProperty("ons.loginfor.err.topic"), propertiesConf.getProperty("ons.loginfor.err.tag"));
            } catch (Exception r) {
                log.error("发送日志消息错误" + "========>");
                r.printStackTrace();
            }
            e.printStackTrace();
            // 出现异常
            return Action.CommitMessage;
        }

        return Action.CommitMessage;
    }





}
